package com.ljsw.activity.ckpage;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ljsw.activity.R;
import com.ljsw.adapter.NotCompletedAdapter;
import com.ljsw.entity.gpk.NotcompeteNumState;
import com.ljsw.entity.gpk.PlateNumbers;
import com.ljsw.entity.pkentity.PKNotCompleteEntity;
import com.ljsw.server.UpServer;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeoutException;

import static com.ljsw.activity.R.id.PKNo;
import static com.ljsw.application.App.app;
import static com.ljsw.application.App.user;

/****
 * 查库的未完成的实时进度
 */
public class CkNotCompletedActivity extends Activity implements View.OnClickListener {
    private static final String TAG = "CkNotCompletedActivity    盘库内容";
    private TextView cknotcompleacuser, cknotcompletetask;//  操作人,任务编号
    private ListView cklistnotcomplete;//listview  设置
    private NotCompletedAdapter nca;//  适配器
    private List<PKNotCompleteEntity> PKNotCompleteList = new ArrayList<PKNotCompleteEntity>();
    private List<NotcompeteNumState> NotcompeteNumStatelist = new ArrayList<NotcompeteNumState>();

    private ImageView cknotpleteactivityblack;///返回键
//      变量

    private String LirbraryNO;//  查库的编号
    private Handler handler;//

    private String userid = "";//  传递操作人
    private List<String> NotCompleLoadDataALLlist = new ArrayList<String>();//接口6.2 获取全部的数据
    private List<NotcompeteNumState> sacnlist = new ArrayList<NotcompeteNumState>(); //  获取扫描到的数据6.1
    private List<NotcompeteNumState> unscanlist = new ArrayList<NotcompeteNumState>();//  获取上面两个集合差值

    private List<PlateNumbers> PlateNumberslist = new ArrayList<PlateNumbers>();// 外层集合
    String username;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ck_not_completed);
        LirbraryNO = getIntent().getStringExtra("PKNO");
        Log.e(TAG, "-----" + PKNo);
        username = user.getName();
        Log.e(TAG, "-----" + username);
        InitView();

        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what) {
                    case 0://   传时间获取数据成功
//                        Log.d("login用户role------------", user.getUserId());
//                        for (int x = 0; x < PKListinfolistlist.size(); x++) {
//
//                            PKListinfolistlist2.add(new PKListinfolistCode(
//                                    PKListinfolistlist.get(x).getLtOrder(),
//                                    PKListinfolistlist.get(x).getName()));
//                        }
//                        for (int i = 0; i < PKListinfolistlist2.size(); i++) {
//                            List<Integer> ltt = new ArrayList<Integer>();
//                            for (int j = 0; j < PKListinfolistlist2.get(i).getLtOrder().size(); j++) {
//                                ltt.add(1);
//                            }
//                            ltCurrent_Order.add(ltt);
//                            if (i == 0) {
//                                ltCurrent_WD.add(1);
//                            } else {
//                                ltCurrent_WD.add(0);
//                            }
//                        }


//                        wa.notifyDataSetInvalidated(); // 更新数据
                        unscanlist.clear();
                        for (int i = 0; i < NotCompleLoadDataALLlist.size(); i++) {
                            if (sacnlist.contains(NotCompleLoadDataALLlist.get(i))) {

                                Log.e(TAG, "没有数据！！！！！！！！！");
                                NotcompeteNumState notcompeteNumState = new NotcompeteNumState();
                                notcompeteNumState.setNotno(NotCompleLoadDataALLlist.get(i));
                                notcompeteNumState.setState("1");
                                unscanlist.add(notcompeteNumState);
                            } else {

                                NotcompeteNumState notcompeteNumState = new NotcompeteNumState();
                                notcompeteNumState.setNotno(NotCompleLoadDataALLlist.get(i));
                                notcompeteNumState.setState("0");
                                unscanlist.add(notcompeteNumState);//  未扫描
                            }
                        }
                        Log.e(TAG, "！！！！！！！！！" + unscanlist.size());

                        if (null == unscanlist) {//  如果当集合为null  提示没有要扫描的数据
                            handler.sendEmptyMessage(11);
                            Log.e(TAG, "没有显示的数据");

                        } else {
                            nca = new NotCompletedAdapter(unscanlist, CkNotCompletedActivity.this);
                            cklistnotcomplete.setAdapter(nca);
                            // 选中条目进行跳转
                            cklistnotcomplete.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                                @Override
                                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                    Intent notcompletin = new Intent(CkNotCompletedActivity.this, CkNotCompletedInfoActivity.class);
                                    notcompletin.putExtra("PKNo", unscanlist.get(position).getNotno());
                                    notcompletin.putExtra("TaskNo", LirbraryNO);
                                    startActivity(notcompletin);

                                }
                            });
                        }
                        break;
                    case 1://登录超时
                        Toast.makeText(CkNotCompletedActivity.this, "正在登陆请检查网络", Toast.LENGTH_SHORT).show();
                        break;
                    case 2://用户不存在
                        Toast.makeText(CkNotCompletedActivity.this, " 获取标牌信息失败", Toast.LENGTH_SHORT).show();
                        break;
                    case 3://异常加载，无法访问服务器
                        Toast.makeText(CkNotCompletedActivity.this, "请求超时", Toast.LENGTH_SHORT).show();
                        break;
                    case 4://账户长度是否正确符合要求 用户不存在
                        Toast.makeText(CkNotCompletedActivity.this, "账户长度不对", Toast.LENGTH_SHORT).show();
                        break;
                    case 5://判断密码的长度密码错误
                        Toast.makeText(CkNotCompletedActivity.this, "请检查密码", Toast.LENGTH_SHORT).show();
                        break;

                    case 7://用户不正确
                        Toast.makeText(CkNotCompletedActivity.this, "请检查用户名", Toast.LENGTH_SHORT).show();
                        break;
                    case 8://用户不正确
                        Toast.makeText(CkNotCompletedActivity.this, "无返回结果", Toast.LENGTH_SHORT).show();
                        break;
                    case 9://用户不正确
                        Toast.makeText(CkNotCompletedActivity.this, "任务号不正确！请返回上一层页面", Toast.LENGTH_SHORT).show();
                        break;
                    case 10://用户不正确
                        Toast.makeText(CkNotCompletedActivity.this, "请检查时间", Toast.LENGTH_SHORT).show();
                        break;
                    case 11://用户不正确
                        Toast.makeText(CkNotCompletedActivity.this, "没有待扫的标牌", Toast.LENGTH_SHORT).show();
                        break;

                    case 12:
                        nca.notifyDataSetChanged();
                        break;
                    default:
                        Toast.makeText(CkNotCompletedActivity.this, "请检查网络服务", Toast.LENGTH_SHORT).show();
                        break;
                }
            }
        };

    }

    /*
    数据请求  调用6.2 接口
     */
    private void CkNotCompletedActivityLoadData() {
        new Thread(new Runnable() {
            public void run() {
                String NotCompleLoadDataALL = "";
                try {
                    NotCompleLoadDataALL = UpServer.repertoryInfo(LirbraryNO, userid);
                    Log.e(TAG, "=========NotCompleLoadDataALL==" + NotCompleLoadDataALL);
                    NotCompleLoadDataALLlist.clear();

                    JSONObject obj = JSONObject.parseObject(NotCompleLoadDataALL);
                    JSONArray arr = obj.getJSONArray("detailList");
                    for (int i = 0; i < arr.size(); i++) {
                        JSONObject jsonArray = arr.getJSONObject(i);
                        String checkId = jsonArray.getString("SCUTCHEONSNUM");
                        NotCompleLoadDataALLlist.add(checkId);
                    }

                    handler.sendEmptyMessage(0);
                } catch (TimeoutException e) {
                    e.printStackTrace();
                    handler.sendEmptyMessage(3);
                } catch (NullPointerException e) {
                    e.printStackTrace();
                    handler.sendEmptyMessage(2);//查无此人信息
                } catch (NumberFormatException e) {
                    e.printStackTrace();
                    handler.sendEmptyMessage(2);//不是数字
                } catch (Exception e) {

                    e.printStackTrace();
                    Log.e("b", "异常" + e);
//                    Log.e("data报文加密失败！", e.getMessage());
                    handler.sendEmptyMessage(2);
                }
            }
        }

        ).

                start();
    }

    private Button ckl_ckno_update;

    private void InitView() {
        ckl_ckno_update = (Button) findViewById(R.id.ckl_ckno_update);//  更新
        ckl_ckno_update.setOnClickListener(this);
        cknotcompleacuser = (TextView) findViewById(R.id.cknotcompleacuser);
        cknotcompleacuser.setText(username);//  设置登陆人
        cknotcompletetask = (TextView) findViewById(R.id.cktaskno);
        cknotcompletetask.setText("" + LirbraryNO);
        cklistnotcomplete = (ListView) findViewById(R.id.cklistnotcomplete);
        PKNotCompleteList.clear();
        cknotpleteactivityblack = (ImageView) findViewById(R.id.cknotpleteactivityblack);
        cknotpleteactivityblack.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.cknotpleteactivityblack:
                CkNotCompletedActivity.this.finish();
                break;

            case R.id.ckl_ckno_update:
                CkNotCompletedActivityLoadData();
                ckNotCompleLoadNotCompleLoadData();

                break;
            default:
                break;
        }

    }

    @Override
    protected void onResume() {
        super.onResume();
        userid = user.getUserId();
        userid = app.userZhanghao;
        CkNotCompletedActivityLoadData();
        ckNotCompleLoadNotCompleLoadData();
    }


    /**
     * 获取数据代码
     */

    /**
     * 6.1 接口的下已扫描到的数据
     */
    private void ckNotCompleLoadNotCompleLoadData() {

        new Thread(new Runnable() {
            public void run() {
                String result = null;
                try {

                    if (null == LirbraryNO || LirbraryNO.equals("")) {
                        handler.sendEmptyMessage(9);
                    } else {

                        sacnlist.clear();
                        result = UpServer.detailInfo(LirbraryNO, userid);
                        JSONObject obj = JSONObject.parseObject(result);
                        JSONArray arr = obj.getJSONArray("plateNumbers");
                        Log.e(TAG, "===========" + arr.size());
                        for (int i = 0; i < arr.size(); i++) {
                            JSONObject jsonArray = arr.getJSONObject(i);
                            String checkId = jsonArray.getString("SCUTCHEONSNUM");
                            NotcompeteNumState notstate = new NotcompeteNumState();
                            notstate.setNotno(checkId);
                            notstate.setState("1");
                            sacnlist.add(notstate);
                        }

                        PKNotCompleteList.clear();//  清空
//                        Gson gson = new Gson();
//                        PlateNumbers[] mTailZerotoEntity = gson.fromJson(result, PlateNumbers[].class);

//                        for (int i = 0; i < mTailZerotoEntity.length; i++) {
//                            PlateNumberslist = Arrays.asList(mTailZerotoEntity);
//                        }
                        Log.e(TAG, "===========" + PlateNumberslist.size());
                        if (PKNotCompleteList != null && !PKNotCompleteList.equals("")) {
                            handler.sendEmptyMessage(0);
                        } else {
                            handler.sendEmptyMessage(2);
                        }


                    }
                } catch (TimeoutException e) {
                    e.printStackTrace();
                    handler.sendEmptyMessage(3);
                } catch (NullPointerException e) {
                    e.printStackTrace();
                    handler.sendEmptyMessage(2);//查无此人信息
                } catch (NumberFormatException e) {
                    e.printStackTrace();
                    handler.sendEmptyMessage(2);//不是数字
                } catch (Exception e) {

                    e.printStackTrace();
                    Log.e("b", "异常" + e);
//                    Log.e("data报文加密失败！", e.getMessage());
                    handler.sendEmptyMessage(2);
                }

            }
        }

        ).

                start();
    }
}
